#!/bin/bash

source /etc/profile
temp_fifo="./temp_fifo"

sql_dir=$1
flag=$2

today=`date +%Y-%m-%d`
workdir=$(cd $(dirname $0); pwd)
log_dir=$workdir/log

source $workdir/config.cfg

[ -e $temp_fifo ] || mkfifo $temp_fifo
exec 5<> $temp_fifo

rm -rf $temp_fifo

for i in `seq 1 8`
do
  echo >&5
done

for sql_file in `ls $sql_dir`
do
  read -u5
  {
    echo "`date '+%Y-%m-%d %H:%M:%S'` -- execute $flag $sql_file "
	  beeline -u "${hive_url}" -n bi-report -f $sql_dir/$sql_file --force=true > $log_dir/$flag/${sql_file}.log 2>&1
	  if [ "$?" -ne "0" ]
	  then
		  echo "execute $flag error,sql : ${sql_file}" >> $log_dir/execute_result_$today
  	fi
    echo >&5
  }&
done
wait

exec 5<&-
exec 5>&-
